package com.fenghongzhang.day014;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MyBottomBar extends LinearLayout implements View.OnClickListener{

    LinearLayout linearLayout;

    List<MyItem> list ;

    int backColor;
    LayoutParams layoutParams;
    public MyBottomBar(Context context) {
        super(context);
        init();
    }

    public MyBottomBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyBottomBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        list = new ArrayList<>();
        setLayoutParams(new ViewGroup.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)));

        backColor = Color.BLACK;
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.bottom_bar_layout, this, true);

        linearLayout = inflate.findViewById(R.id.bar_ll);
        linearLayout.setBackgroundColor(backColor);
        layoutParams = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.weight = 1;
        layoutParams.gravity = Gravity.CENTER;

    }

    public MyBottomBar addItem(MyItem myItem){
        list.add(myItem);
        return this;
    }

    @Override
    public void onClick(View v) {
        int postion = (int) v.getTag(R.id.barId);
        myListener.selectedClick(postion);
    }


    public void setBackColor(int backColor) {
        this.backColor = backColor;
        linearLayout.setBackgroundColor(backColor);
//        requestLayout();
    }

    public interface MyListener{
        void selectedClick(int postion);
    }
    private MyListener myListener;

    public void setMyListener(MyListener myListener) {
        this.myListener = myListener;
    }


    //变颜色
    public void selected(int postion){
        for (int i = 0; i < list.size(); i++) {
            MyItem myItem = list.get(i);
            String name = myItem.getName();
            if(name != null){
                if(i == postion){
                    TextView textView = (TextView) linearLayout.getChildAt(i); //textView
                    textView.setTextColor(Color.WHITE);
                }else{
                    TextView textView = (TextView) linearLayout.getChildAt(i); //textView
                    textView.setTextColor(Color.RED);
                }
            }
        }
    }




    //把所有的item添加到布局中
    public void build(){
        for (int i = 0; i < list.size(); i++) {
            //文字,一个是图片
            MyItem myItem = list.get(i);
            if(myItem.getName() != null){
                TextView textView = new TextView(getContext());
                textView.setText(myItem.getName());
                textView.setTextSize(18);
                textView.setTextColor(Color.RED);
                textView.setGravity(Gravity.CENTER);
                textView.setLayoutParams(layoutParams);
                textView.setTag(R.id.barId,i);
                textView.setOnClickListener(this);
                linearLayout.addView(textView);
            }else{
                ImageView imageView = new ImageView(getContext());
                imageView.setImageDrawable(myItem.getDrawable());
                imageView.setLayoutParams(layoutParams);
                imageView.setTag(R.id.barId,i);
                imageView.setOnClickListener(this);
                linearLayout.addView(imageView);
            }


        }
    }

}
